//
//  SrEcgAlgorithmResults.m
//  sr01sdkProject
//
//  Created by Linktop on 2024/3/5.
//  算法结果

#import "SrEcgAlgorithmResults.h"

@implementation SrEcgAlgorithmResults

-(instancetype)initWithData:(NSData *)data
{
    if (self = [super init]) {
        
        uint8_t * bytes = (uint8_t *)[data bytes];
        self.heartRate = bytes[2];
        
        uint8_t aarChk = bytes[3];
        [self aarcheckAnaly:aarChk];
        
        uint16_t rmssd = *((uint16_t *)(bytes + 4));
        _rmssd = rmssd;
        
        _sdnn = *((uint16_t *)(bytes + 6));
        _stress = bytes[8];  // 压力指数 0-100有效 0XFF表示无效
        
        uint16_t bmr = *((uint16_t *)(bytes + 9));
        _bmr = bmr;
        
        _activeCal = *((uint16_t *)(bytes + 11));
        
        uint8_t usrPrsn = bytes[13];
        
        _ecgSq = usrPrsn & 0X0F; // 信号质量
        
        _present = (usrPrsn >> 4) & 0X01;
        _alive = (usrPrsn >> 5) & 0X01;
        _avgHeartrate = bytes[14] & 0xFF; // 平均心率
        
        // progressType
        _progressType = bytes[15] & 0xFF; // 进度类型
        _progress = bytes[16] & 0xFF; // 0 - 100 进度

    }
    return self;
}

-(void)aarcheckAnaly:(uint8_t)aarChk {
   
    
    uint8_t arrhythmiaTest = aarChk & 0X0F; //心率失常检查
    _arrhythmia = arrhythmiaTest;
    
    // 心电图信号振幅很低，请确保接触面清洁，然后重试。
    _low_amplitude = (arrhythmiaTest >> 4) & 0X01;
    
    // 心电图信号有明显的噪音，请确保设备没有握得太紧，然后重试。
    _significant_noise = (arrhythmiaTest >> 5) & 0X01;
    
    // 心电图信号似乎不稳定，请确保您保持静止，
    _unstable_signal = (arrhythmiaTest >> 6) & 0X01;
    // 没有足够的数据，请确保遵循心电图记录指南
    _not_enough_data = (arrhythmiaTest >> 7) & 0X01;

}

@end
